<?php

class banerActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        $c = new Criteria();
        $c->addAscendingOrderByColumn(BanerPeer::ID);

        $pager = new sfPropelPager('Baner', sfConfig::get('app_max_per_page'));
        $pager->setCriteria($c);
        $pager->setPage($request->getParameter('page', 1));
        $pager->init();
        $this->pager = $pager;
    }

    public function executeNew(sfWebRequest $request){

    }

    public function executeNew_save(sfWebRequest $request){


        $titulo = $request->getParameter('titulo');
        $imagen = $_FILES["imagen"]["tmp_name"];

        $tamaño=BanerPeer::doCount(new Criteria)+1;

        $baner = new Baner();
        $baner->setTitulo($titulo);
        $baner->save();

        if($imagen!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($imagen, sfConfig::get('sf_upload_dir').$DS."baner".$DS.$baner->getId().".jpg");
        }

        $this->getUser()->setFlash('msg','Baner creada satisfactoriamente');
        $this->redirect('@baner');
    }

    public function executeEdit(sfWebRequest $request){
        $id = $request->getParameter('id');
        $this->baner = BanerPeer::retrieveByPK($id);
    }

    public function executeEdit_save(sfWebRequest $request){
        $id = $request->getParameter('id');
        $titulo = $request->getParameter('titulo');
        $imagen = $_FILES["imagen"]["tmp_name"];

        
        $baner = BanerPeer::retrieveByPK($id);
        $baner->setTitulo($titulo);
        $baner->save();

        if($imagen!=''){
            $DS = DIRECTORY_SEPARATOR;
            move_uploaded_file($image, sfConfig::get('sf_upload_dir').$DS."baner".$DS.$baner->getId().".jpg");
        }

        $this->getUser()->setFlash('msg','Baner modificada satisfactoriamente');
        $this->redirect('@baner');
    }

    public function executeDelete(sfWebRequest $request){
        $id = $request->getParameter('id');
        $baner = BanerPeer::retrieveByPK($id);
        $baner->delete();

        $this->getUser()->setFlash('msg','Baner eliminada satisfactoriamente');
        $this->redirect('@baner');
    }

}